//
//  KRCommentCell.m
//  cookBook
//
//  Created by kuroken on 16/1/16.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRCommentCell.h"
#import <UIButton+WebCache.h>
#import <PureLayout.h>
#import "KRConst.h"
#import "KRTextView.h"
#import "KRReadComment.h"

// cell的标识
static NSString * const reuseIdentifier = @"comment";
// 头像的大小
static NSInteger const avatarImageWH = 40;

@interface KRCommentCell ()

/** 头像Button */
@property (nonatomic, weak) UIButton *avatarBtn;
/** 用户名Label */
@property (nonatomic, weak) UILabel *nameLabel;
/** 时间Label */
@property (nonatomic, weak) UILabel *timeLabel;
/** 回复Label */
@property (nonatomic, weak) UILabel *replyLabel;
/** 评论TextView */
@property (nonatomic, weak) KRTextView *commentTextView;
/** 引用评论TextView */
@property (nonatomic, weak) KRTextView *atContentTextView;
/** 分隔线 */
@property (nonatomic, weak) UIView *lineView;
/** 评论TextView约束 */
@property (nonatomic, strong) NSArray<NSLayoutConstraint *> *atCommentConstraints;
/** 分隔线约束 */
@property (nonatomic, strong) NSLayoutConstraint *lineViewConstraintWithCommment;
@property (nonatomic, strong) NSLayoutConstraint *lineViewConstraintWithAtCommment;

/** 引用评论背景 */
@property (nonatomic, weak) UIImageView *atContentImageView;
/** 引用评论背景约束 */
@property (nonatomic, strong) NSArray<NSLayoutConstraint *> *atContentImageConstraintWithComment;

@end

@implementation KRCommentCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KRCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubview];
        [self addConstraint];
    }
    return self;
}

- (void)setupSubview
{
    UIButton *avatarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:avatarBtn];
    self.avatarBtn =avatarBtn;
    
    UILabel *nameLabel = [[UILabel alloc] init];
    [nameLabel setFont:[UIFont systemFontOfSize:14]];
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    [timeLabel setFont:[UIFont systemFontOfSize:13]];
    [timeLabel setTextColor:[UIColor lightGrayColor]];
    [self.contentView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UILabel *replyLabel = [[UILabel alloc] init];
    [replyLabel setText:@"回复"];
    [replyLabel setFont:[UIFont systemFontOfSize:14]];
    [replyLabel setTextColor:KRColor(111, 143, 18)];
    [self.contentView addSubview:replyLabel];
    self.replyLabel = replyLabel;
    
    KRTextView *commentTextView = [[KRTextView alloc] init];
    commentTextView.textContainerInset = UIEdgeInsetsMake(0, -5, 0, -5);
    [self.contentView addSubview:commentTextView];
    self.commentTextView = commentTextView;
    
    // 引用评论背景图片
    UIImageView *atContentImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:atContentImageView];
    self.atContentImageView = atContentImageView;
    
    KRTextView *atContentTextView = [[KRTextView alloc] init];
    atContentTextView.textContainerInset = UIEdgeInsetsMake(15, 5, 5, 0);
    [self.contentView addSubview:atContentTextView];
    self.atContentTextView = atContentTextView;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = KRColor(188, 187, 192);
    [self.contentView addSubview:lineView];
    self.lineView = lineView;
}

- (void)addConstraint
{
    // 头像Button约束
    [self.avatarBtn autoSetDimensionsToSize:CGSizeMake(avatarImageWH, avatarImageWH)];
    [self.avatarBtn autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:15];
    [self.avatarBtn autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
    self.avatarBtn.layer.cornerRadius = avatarImageWH * 0.5;
    self.avatarBtn.clipsToBounds = YES;
    
    // 用户名Label约束
    [self.nameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.avatarBtn withOffset:-2];
    [self.nameLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.avatarBtn withOffset:10];
    
    // 时间Label约束
    [self.timeLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.nameLabel withOffset:2];
    [self.timeLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.nameLabel];

    // 回复Label约束
    [self.replyLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.nameLabel];
    [self.replyLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];

    // 评论TextView约束
    [self.commentTextView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.avatarBtn withOffset:10];
    [self.commentTextView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.nameLabel];
    [self.commentTextView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];

    // 引用评论TextView约束
    NSMutableArray<NSLayoutConstraint *> *atCommentConstraints = [NSMutableArray array];
    [atCommentConstraints addObject:[self.atContentTextView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.commentTextView]];
    [atCommentConstraints addObject:[self.atContentTextView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.commentTextView]];
    [atCommentConstraints addObject:[self.atContentTextView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.commentTextView]];
    self.atCommentConstraints = atCommentConstraints;

    // atContentImageView　背景
    NSMutableArray<NSLayoutConstraint *> *atContentImageConstraintWithComment = [NSMutableArray array];
    [atContentImageConstraintWithComment addObject:[self.atContentImageView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.atContentTextView withOffset:0]];
    [atContentImageConstraintWithComment addObject:[self.atContentImageView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.atContentTextView withOffset:0]];
    [atContentImageConstraintWithComment addObject:[self.atContentImageView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.atContentTextView withOffset:0]];
    [atContentImageConstraintWithComment addObject:[self.atContentImageView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.atContentTextView withOffset:0]];
    self.atContentImageConstraintWithComment = atContentImageConstraintWithComment;
    
    // 分割线约束
    [NSLayoutConstraint autoSetPriority:UILayoutPriorityDefaultLow forConstraints:^{
        self.lineViewConstraintWithCommment = [self.lineView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.commentTextView withOffset:20];
    }];
    [NSLayoutConstraint autoSetPriority:UILayoutPriorityDefaultHigh forConstraints:^{
        self.lineViewConstraintWithAtCommment = [self.lineView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.atContentTextView withOffset:20];
    }];
    [self.lineView autoSetDimensionsToSize:CGSizeMake(KRScreenWidth, 0.3)];
    [self.lineView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.avatarBtn];
    [self.lineView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.contentView withOffset:0];
}

/**
 *  设置评论数据
 */
- (void)setReadComment:(KRReadComment *)readComment
{
    _readComment = readComment;
    
    [self.avatarBtn sd_setImageWithURL:[NSURL URLWithString:readComment.comment_avatar] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"headico_big"]];
    
    [self.nameLabel setText:readComment.comment_userName];
    [self.timeLabel setText:readComment.comment_createTime];
    [self.commentTextView setAttributedText:readComment.attributed_comment_content];
    
    if (readComment.comment_atContent.length) { // 有引用
        // 引用评论
        self.atContentTextView.hidden = NO;
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"引用:%@\n", readComment.comment_atUserName]];
        [attributedText appendAttributedString:readComment.attributed_comment_atContent];
        [attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, attributedText.length)];
        [attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, attributedText.length)];

        [self.atContentTextView setAttributedText:attributedText];
        if (![self.contentView.constraints containsObject:self.atCommentConstraints[0]]) {
            [self.atCommentConstraints autoInstallConstraints];
        }
        // 背景图片
        self.atContentImageView.hidden = NO;
        [self.atContentImageView setImage:[UIImage imageNamed:@"bg_arrowup_radius"]];
        if (![self.contentView.constraints containsObject:self.atContentImageConstraintWithComment[0]]) {
            [self.atContentImageConstraintWithComment autoInstallConstraints];
        }
        // 更新分割线约束
        if ([self.contentView.constraints containsObject:self.lineViewConstraintWithCommment]) {
            [self.lineViewConstraintWithCommment autoRemove];
        }
        if (![self.contentView.constraints containsObject:self.lineViewConstraintWithAtCommment]) {
            [self.lineViewConstraintWithAtCommment autoInstall];
        }

    } else { // 没有引用
        // 引用评论
        self.atContentTextView.hidden = YES;
        [self.atContentTextView setText:nil];
        if ([self.contentView.constraints containsObject:self.atCommentConstraints[0]]) {
            [self.atCommentConstraints autoRemoveConstraints];
        }
        
        // 背景图片
        self.atContentImageView.hidden = YES;
        [self.atContentImageView setImage:nil];
        if ([self.contentView.constraints containsObject:self.atContentImageConstraintWithComment[0]]) {
            [self.atContentImageConstraintWithComment autoRemoveConstraints];
        }

        // 更新分割线约束
        if (![self.contentView.constraints containsObject:self.lineViewConstraintWithCommment]) {
            [self.lineViewConstraintWithCommment autoInstall];
        }
        if ([self.contentView.constraints containsObject:self.lineViewConstraintWithAtCommment]) {
            [self.lineViewConstraintWithAtCommment autoRemove];
        }
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
